package com.link.base.base.trackchild.service;
/**
 * @author: 秦学刚
 * @date: 2018-11-21
 * @comments:
 */

import javax.annotation.Resource;

import com.link.base.user.model.CoreUser;
import com.link.base.base.trackchild.dao.mybatis.mapper.TrackchildMapper;
import com.link.base.base.trackchild.model.Trackchild;
import com.link.core.util.JsonUtil;
import com.link.core.util.RedisUtil;
import com.link.core.util.UserUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Service;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;


import java.util.*;


@Service
public class TrackchildServiceImpl extends BasicServiceImpl<Trackchild> implements TrackchildService {

    @Resource
    private TrackchildMapper trackchildMapper;

    @Override
    public BasicMapper<Trackchild> getBasicMapper() {
        return trackchildMapper;
    }

    @Override
    public void loadTrackToRedis() throws Exception {
        LinkRedisClient jedis = RedisUtil.getJedis();
        try {
            CoreUser user = UserUtil.getUser();
            jedis.del("TRACK_CONFIG_LIST_" + user.getCorpid());
            Trackchild trackchild = new Trackchild();
            trackchild.setCorpid(user.getCorpid());
            List<Trackchild> list = trackchildMapper.queryHeadAndChild(trackchild);
            List<Trackchild> temp = new ArrayList<>();
            for (Trackchild trackchild1 : list) { // 值列表类型不可以转成小写
                trackchild1.setTrackCode(trackchild1.getTrackCode().toLowerCase().trim());
                trackchild1.setTrackNew(trackchild1.getTrackNew().toLowerCase().trim());
                trackchild1.setTrackDel(trackchild1.getTrackDel().toLowerCase().trim());
                trackchild1.setTrackUpd(trackchild1.getTrackUpd().toLowerCase().trim());
                trackchild1.setFieldCode(trackchild1.getFieldCode().toLowerCase().trim());
                // trackchild1.setFieldType(trackchild1.getFieldType().toLowerCase());
                temp.add(trackchild1);
            }
            Map<String, String> map = new HashMap<>(30);
            List<Trackchild> tempList = new ArrayList<>();
            if (temp != null && temp.size() > 0) {
                for (int i = 0; i < temp.size(); i++) {
                    if (i == 0) {
                        tempList.add(temp.get(0));
                        if (temp.size() == 1 && temp.get(0).getTrackCode() != null) {
                            map.put(temp.get(0).getTrackCode().toLowerCase(), JsonUtil.toString(tempList));
                        }
                    } else {
                        if (temp.get(i).getTrackCode().equals(temp.get(i - 1).getTrackCode())) {
                            tempList.add(temp.get(i));
                            if (i == temp.size() - 1 && temp.get(i).getTrackCode() != null) {
                                map.put(temp.get(i).getTrackCode().toLowerCase(), JsonUtil.toString(tempList));
                            }
                        } else {
                            map.put(temp.get(i - 1).getTrackCode().toLowerCase(), JsonUtil.toString(tempList));
                            tempList = new ArrayList<>();
                            tempList.add(temp.get(i));
                        }
                    }
                }
            }
            if (map.size() > 0) {
                jedis.hmset("TRACK_CONFIG_LIST_" + user.getCorpid(), map);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

}

